<template>
    <el-dialog :visible.sync="visible" width="800px" append-to-body :title="'招标人代表信息-' + (mode == 0 ? '新增' : '修改')">
        <el-form ref="form" :model="form" label-width="100px" :rules="rules">
            <el-form-item label="招标人代表名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="公司名称" prop="company">
                <el-input v-model="form.company"></el-input>
            </el-form-item>
            <el-form-item label="专业类别" prop="professionType">
                <el-select
                    v-model="form.professionType"
                    placeholder="请选择专业类别"
                    style="width: 660px"
                >
                    <el-option
                    v-for="type in professionTypeOptions"
                    :key="type.value"
                    :label="type.label"
                    :value="type.value"
                    > </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="职称" prop="profession">
                <el-input v-model="form.profession"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="telphone">
                <el-input v-model="form.telphone" type="number"></el-input>
            </el-form-item>
            <el-form-item label="身份证号" prop="idcard">
                <el-input v-model="form.idcard"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">确定</el-button>
                <el-button @click="onCancel">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import { addAgent, editAgent } from "@/api/unit/random"

export default {
    props: ["modalVisible", "mode", "current", "refresh"],
    computed: {
        visible: {
            get() {
                return this.modalVisible
            },
            set(value) {
                this.$emit("update:modalVisible", value);
            }
        }
    },
    data() {
        return {
            form: {},
            rules: {
                company: [{
                    required: true,
                    message: "请填写公司名称"
                }],
                name: [{
                    required: true,
                    message: "请填写招标人代表名称"
                }],
                professionType: [{
                    required: true,
                    message: "请选择专业类别"
                }],
                profession: [{
                    required: true,
                    message: "请填写招标人代表职称"
                }],
                telphone: [{
                    required: false,
                    message: "请填写招标人代表电话"
                }],
                idcard: [{
                    required: false,
                    message: "请填写招标人代表身份证号"
                }]
            },
            professionTypeOptions: [
                {
                label: "专用类别",
                value: "专用类别",
                },
                {
                label: "通用类别",
                value: "通用类别",
                },
            ]
        }
    },
    methods: {
        submit() {
            this.$refs.form.validate().then(() => {
                if (this.mode == 0) {
                    addAgent(this.form).then((res)=>this.process(res));
                } else {
                    editAgent(this.form).then((res)=>this.process(res));
                }

            });
        },
        process(res) {
            if (res.code == 200) {
                this.$message.success(res.msg);
                this.visible = false;
                this.refresh();
            } else {
                this.$message.error(res.msg);
            }
        },
        onCancel() {
            this.visible = false;
        }
    },
    watch: {
        modalVisible() {
            if (this.modalVisible) {
                if (this.mode == 1) this.form = { ...this.current };
                else {
                    this.form = {};
                    this.$refs.form?.resetFields();
                }
            }
        }
    }
}
</script>